package com.pengtu.gsj.controller.checker.yh.equipment;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.CheckTableBaseController;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.yh.equipment.TechnicalTrainingService;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * 国检-技术培训检查表控制层
 * TechnicalTrainingController
 * 
 * 2017年7月26日 下午3:32:28
 * @author zl
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("technicalTrain")
public class TechnicalTrainingController extends CheckTableBaseController {
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private DownService downService;
	@Autowired
	private TechnicalTrainingService technicalTrainingService;
	
	/**
	 * 
	 * showTechnicalTraining:
	 * 适用:跳转到技术培训检查表页面
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Token(save = true)
	@RequestMapping("show")
	public String showTechnicalTraining(Model model) {
		//获取提示信息并返回到页面显示
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "check/yh/equipment/technicalTrain";
	}
	
	/**
	 * 
	 * saveTechnicalTraining:
	 * 适用:保存技术培训检查表内容，包括搜索条件的存储
	 * @param checkTable
	 * @param checkProject
	 * @param attributes
	 * @return 绑定页面数据（left  parentId projectId menuId leftChild） 重定向到检查表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@Token(remove = true)
	@RequestMapping("save")
	public String saveTechnicalTraining(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, RedirectAttributes attributes){
		checkTable.setCheckProject(checkProject);
		checkTable.setTableFull(checkTable.getYearFull()); //设置当前检查表格的总分
		checkTableService.save(checkTable);
		attributesParams(checkTable, attributes);
		return "redirect:/technicalTrain/show.do";
	}
	
	
	/**
	 * 
	 * getSearchList:
	 * 适用:ajax请求，根据页面的搜索条件检索数据
	 * @param checkTable
	 * @param checkProject
	 * @param sysmenu
	 * @return 返回检索结果
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value="search", produces = "application/json; charset=utf-8")
	public Map<String, Object> getSearchList(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, @ModelAttribute Sysmenu sysmenu) throws Exception{
		return technicalTrainingService.getCheckContent(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, sysmenu == null ? "" : sysmenu.getTag());
	}
	
	/**
	 * 
	 * downWordAndZip:
	 * 适用:检查表的一键导出，包括检查表的内容及检查条件所对应的附件
	 * @param checkProject
	 * @param checkTable
	 * @param sysmenu
	 * @return  返回打包好的压缩包供用户下载
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("downWord")
	public ResponseEntity<byte[]> downWordAndZip(@ModelAttribute CheckProject checkProject, @ModelAttribute CheckTable checkTable, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String sysName = sysmenu == null ? "" : sysmenu.getName();
		//根据搜索条件查询出来的相关结果
		resultMap =  technicalTrainingService.getCheckContent(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, sysmenu == null ? "" : sysmenu.getTag());
		return downService.downWordAndZip(SpringMvcUtils.getParameter("checkSubmoduleName"), SpringMvcUtils.getParameter("checkModuleName"), sysName, "maintenance/技术培训.xml", resultMap);
	}
}
